package com.cardbaobao.cardbabyclient.ui.fragment.newindexitem;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.adapter.PopularityRcAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.model.IndexLayout;
import com.cardbaobao.cardbabyclient.widget.RecyclerViewWithFooter.OnLoadMoreListener;
import com.cardbaobao.cardbabyclient.widget.RecyclerViewWithFooter.OnTopListener;
import com.cardbaobao.cardbabyclient.widget.RecyclerViewWithFooter.RecyclerViewWithFooter;

import java.util.List;

import butterknife.BindView;

/**
 * Description:
 * Created by FHW
 * on 2017/6/12
 */

public class PopularityFragment extends BaseFragment implements OnLoadMoreListener {

    @BindView(R.id.rv_popularity)
    RecyclerViewWithFooter rv_popularity;

    private PopularityRcAdapter mAdapter;

    private OnTopListener onTopListener;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_popularity;
    }

    @Override
    protected void initViewFinish(View view, Bundle savedInstanceState) {

        mAdapter = new PopularityRcAdapter(getActivity());
        rv_popularity.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_popularity.setOnLoadMoreListener(this);
        rv_popularity.setAdapter(mAdapter);

        rv_popularity.onScrollListener(new OnTopListener() {
            @Override
            public void onTop() {
                if (onTopListener != null)
                    if (mAdapter.getItemCount() > 3) {
                        onTopListener.onTop();
                    }
            }
        });

    }

    public void onTopListener(OnTopListener onTopListener) {
        this.onTopListener = onTopListener;
    }

    @Override
    public void onLoadMore() {
        rv_popularity.setEnd("没有更多数据");
    }

    public void setData(List<IndexLayout.HomeTabBean> popularityList) {
        if (mAdapter == null) {
            mAdapter = new PopularityRcAdapter(getActivity());
        }
        mAdapter.setData(popularityList);
    }

}
